package com.opo5.logisticsmanagement.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.opo5.logisticsmanagement.Vo.RoleMenuParamVo;
import com.opo5.logisticsmanagement.common.R;
import com.opo5.logisticsmanagement.dao.RoleMenuDao;
import com.opo5.logisticsmanagement.domain.RoleMenu;
import com.opo5.logisticsmanagement.service.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author lixin
* @description 针对表【tbs_role_menu】的数据库操作Service实现
* @createDate 2022-07-10 08:55:41
*/
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuDao, RoleMenu>
    implements RoleMenuService {

    @Autowired
    private RoleMenuDao roleMenuDao;

    @Override
    @Transactional
    public R editRoleMenu(RoleMenuParamVo roleMenuParamVo) {
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleMenuParamVo.getId());
        roleMenuDao.delete(queryWrapper);
        int count = 0;
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setRoleId(roleMenuParamVo.getId());
        for (Integer menuId : roleMenuParamVo.getMenuIds()){
            roleMenu.setMenuId(menuId);
            count+=roleMenuDao.insert(roleMenu);
        }
        if (count!= roleMenuParamVo.getMenuIds().length){
            throw new RuntimeException("更新失败");
        }
        return R.success(null);
    }
}




